package slxy.jiangtao.dts.core.util;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

/**
 * Code must be sexy
 * Created by Jiang Tao on 2017/5/3.
 * 说明：
 */
public class JacobUtil {

    public static boolean wordToHtml (String inPath,String toPath) {
        //启动word
        ActiveXComponent axc = new ActiveXComponent("Word.Application");
        boolean flag = false;
        try {
            //后台执行，不显示word界面
            axc.setProperty("Visible",new Variant(false));

            Dispatch docs = axc.getProperty("Documents").toDispatch();

            //打开word文档
            Dispatch doc = Dispatch.invoke(docs,"Open",Dispatch.Method,
                    new Object[]{inPath,new Variant(false),
                            new Variant(true)},new int[1]).toDispatch();

            //作为html保存到文件
            Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
                    toPath, new Variant(10) }, new int[1]);     //这里的new Variant(10)中的初始化值是10，而不是8
            Variant f = new Variant(false);
            Dispatch.call(doc, "Close", f);
            flag = true;
            return flag;

        } catch (Exception e) {
            e.printStackTrace();
            return flag;
        } finally{
            axc.invoke("Quit", new Variant[] {});
        }
    }

}
